# Generate F3DIcon buffer
include("f3dEmbed")

f3d_embed_file(
  INPUT "${F3D_SOURCE_DIR}/resources/logo32.png"
  NAME F3DIcon
  BINARY)

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/F3DConfig.h.in"
    "${CMAKE_CURRENT_BINARY_DIR}/F3DConfig.h")

set(F3D_SOURCE_FILES
  ${CMAKE_CURRENT_SOURCE_DIR}/F3DConfigFileTools.cxx
  ${CMAKE_CURRENT_BINARY_DIR}/F3DIcon.cxx
  ${CMAKE_CURRENT_SOURCE_DIR}/F3DOptionsParser.cxx
  ${CMAKE_CURRENT_SOURCE_DIR}/F3DStarter.cxx
  ${CMAKE_CURRENT_SOURCE_DIR}/F3DSystemTools.cxx
  ${CMAKE_CURRENT_SOURCE_DIR}/main.cxx
)

if(WIN32)
  list(APPEND F3D_SOURCE_FILES "${F3D_SOURCE_DIR}/resources/f3d.rc")
endif()

if(APPLE)
  list(APPEND F3D_SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/F3DNSDelegate.mm")
endif()

# Define f3d target
add_executable(f3d ${F3D_SOURCE_FILES})
target_link_libraries(f3d PUBLIC libf3d)

if(UNIX AND NOT APPLE)
  option(F3D_LINUX_APPLICATION_LINK_FILESYSTEM "Link with libstdc++fs" ON)
  mark_as_advanced(F3D_LINUX_APPLICATION_LINK_FILESYSTEM)
  if(F3D_LINUX_APPLICATION_LINK_FILESYSTEM)
    target_link_libraries(f3d PRIVATE stdc++fs)
  endif()
endif()

set_target_properties(f3d PROPERTIES
  CXX_VISIBILITY_PRESET hidden
  CXX_STANDARD 17
  )

if (APPLE)
  if (F3D_MACOS_BUNDLE)
    set_target_properties(f3d PROPERTIES INSTALL_RPATH @loader_path/../../../${CMAKE_INSTALL_LIBDIR})
  else ()
    set_target_properties(f3d PROPERTIES INSTALL_RPATH @loader_path/../${CMAKE_INSTALL_LIBDIR})
  endif ()
elseif (UNIX)
  set_target_properties(f3d PROPERTIES INSTALL_RPATH $ORIGIN/../${CMAKE_INSTALL_LIBDIR})
elseif (MSVC)
  set_property(DIRECTORY ${F3D_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT f3d) # Set f3d as startup project in visual studio
  set_target_properties(f3d PROPERTIES VS_DEBUGGER_ENVIRONMENT "PATH=$<TARGET_FILE_DIR:VTK::CommonCore>") # Add VTK in MSVC PATH environment variable
endif ()

target_include_directories(f3d PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
if (F3D_USE_EXTERNAL_CXXOPTS)
  target_link_libraries(f3d PRIVATE cxxopts::cxxopts)
else ()
  target_include_directories(f3d PUBLIC $<BUILD_INTERFACE:${F3D_SOURCE_DIR}/external/cxxopts>)
endif ()

if (F3D_USE_EXTERNAL_NLOHMANN_JSON)
  target_link_libraries(f3d PRIVATE nlohmann_json::nlohmann_json)
else ()
  target_include_directories(f3d PUBLIC $<BUILD_INTERFACE:${F3D_SOURCE_DIR}/external/nlohmann_json>)
endif ()

set(f3d_compile_options_private "")
set(f3d_compile_options_public "")
set(f3d_link_options_public "")

# Headless EGL build
if (VTK_OPENGL_HAS_EGL)
  target_compile_definitions(f3d PRIVATE F3D_HEADLESS_BUILD)
endif ()

# F3D_STRICT_BUILD
list(APPEND f3d_compile_options_private ${f3d_strict_build_compile_options})
if(F3D_STRICT_BUILD)
  if(MSVC)
    target_compile_definitions(f3d PRIVATE _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS _CRT_SECURE_NO_WARNINGS)
  elseif(APPLE)
    target_compile_definitions(f3d PRIVATE GL_SILENCE_DEPRECATION)
  endif()
endif()

# Coverage
list(APPEND f3d_compile_options_public ${f3d_coverage_compile_options})
list(APPEND f3d_link_options_public ${f3d_coverage_link_options})

# Sanitizer
list(APPEND f3d_compile_options_public ${f3d_sanitizer_compile_options})
list(APPEND f3d_link_options_public ${f3d_sanitizer_link_options})

# MacOS Bundle app
if(F3D_MACOS_BUNDLE)
  set_target_properties(f3d PROPERTIES MACOSX_BUNDLE TRUE)

  # Add logo icon
  set(MACOSX_BUNDLE_ICON_FILE logo.icns)
  set(f3d_ICON ${F3D_SOURCE_DIR}/resources/logo.icns)
  set_source_files_properties(${f3d_ICON} PROPERTIES
    MACOSX_PACKAGE_LOCATION "Resources")
  target_sources(f3d PRIVATE ${f3d_ICON})

  # Set other MacOS vars
  set(MACOSX_BUNDLE_EXECUTABLE_NAME ${PROJECT_NAME})
  set(MACOSX_BUNDLE_INFO_STRING ${PROJECT_DESCRIPTION})
  set(MACOSX_BUNDLE_GUI_IDENTIFIER "app.f3d.F3D")
  set(MACOSX_BUNDLE_LONG_VERSION_STRING ${F3D_VERSION})
  set(MACOSX_BUNDLE_BUNDLE_NAME ${PROJECT_NAME})
  set(MACOSX_BUNDLE_SHORT_VERSION_STRING ${F3D_VERSION})
  set(MACOSX_BUNDLE_BUNDLE_VERSION ${F3D_VERSION})
  set(MACOSX_BUNDLE_COPYRIGHT "Michael Migliore, Mathieu Westphal")

  # Generate MacOS bundle
  configure_file("${F3D_SOURCE_DIR}/resources/BundleInfo.plist.in"
    "${CMAKE_CURRENT_BINARY_DIR}/BundleInfo.plist")
  set_target_properties(f3d PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_BINARY_DIR}/BundleInfo.plist")
endif()

if(APPLE)
  target_link_libraries(f3d PRIVATE "-framework Cocoa")
endif()

# Windows executable without console
if(WIN32)
  if(F3D_WINDOWS_GUI)
    set_target_properties(f3d PROPERTIES WIN32_EXECUTABLE TRUE)
  else()
    # force usage of wWinMainCRTStartup in console mode for Unicode support
    list(APPEND f3d_link_options_public "/ENTRY:wWinMainCRTStartup")
  endif()
endif()

target_compile_options(f3d PUBLIC ${f3d_compile_options_public} PRIVATE ${f3d_compile_options_private})
target_link_options(f3d PUBLIC ${f3d_link_options_public})

# Create man
if(UNIX AND NOT APPLE)
  option(F3D_LINUX_GENERATE_MAN "Generate and install a man entry for f3d using help2man" OFF)
  mark_as_advanced(F3D_LINUX_GENERATE_MAN)
  if(F3D_LINUX_GENERATE_MAN)
    find_program(HELP2MAN help2man)
    find_program(GZIP gzip)
    if(HELP2MAN AND GZIP)
      set(MAN_OUTPUT_FILE ${CMAKE_BINARY_DIR}/f3d.1)
      add_custom_command(
        OUTPUT ${MAN_OUTPUT_FILE}
        COMMAND ${HELP2MAN} $<TARGET_FILE:f3d> -N -n "fast and minimalist 3D viewer" > ${MAN_OUTPUT_FILE}
        COMMAND ${GZIP} -f ${MAN_OUTPUT_FILE}
        DEPENDS f3d)
      add_custom_target(man ALL DEPENDS ${MAN_OUTPUT_FILE})

      install(FILES "${CMAKE_BINARY_DIR}/f3d.1.gz"
        DESTINATION "share/man/man1/" COMPONENT documentation)
    else()
      message(WARNING "help2man or gzip not found, cannot create man entry")
    endif()
  endif()
endif()

# Testing
if(BUILD_TESTING)
  add_subdirectory(testing)
endif()

# Installing
install(TARGETS f3d
  EXPORT f3dTargets
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT application
  BUNDLE DESTINATION "." COMPONENT application)

# Default config files
install(
  DIRECTORY "${F3D_SOURCE_DIR}/resources/configs/"
  DESTINATION "${f3d_config_dir}"
  COMPONENT configuration
  EXCLUDE_FROM_ALL)

# Other ressoure files
if(UNIX AND NOT APPLE AND NOT ANDROID)
  install(FILES "${F3D_SOURCE_DIR}/resources/f3d.desktop"
    DESTINATION "share/applications" COMPONENT assets)
  install(FILES "${F3D_SOURCE_DIR}/resources/logo32.png"
    DESTINATION "share/icons/hicolor/32x32/apps" COMPONENT assets
    RENAME "f3d.png")
  install(FILES "${F3D_SOURCE_DIR}/resources/logo48.png"
    DESTINATION "share/icons/hicolor/48x48/apps" COMPONENT assets
    RENAME "f3d.png")
  install(FILES "${F3D_SOURCE_DIR}/resources/logo64.png"
    DESTINATION "share/icons/hicolor/64x64/apps" COMPONENT assets
    RENAME "f3d.png")
  install(FILES "${F3D_SOURCE_DIR}/resources/logo.svg"
    DESTINATION "share/icons/hicolor/scalable/apps" COMPONENT assets
    RENAME "f3d.svg")
  install(FILES "${F3D_SOURCE_DIR}/resources/completion.bash"
    DESTINATION "share/bash-completion/completions" COMPONENT shellext
    RENAME "f3d")
  install(FILES "${F3D_SOURCE_DIR}/resources/completion.zsh"
    DESTINATION "share/zsh/site-functions" COMPONENT shellext
    RENAME "_f3d")
  install(FILES "${F3D_SOURCE_DIR}/resources/completion.fish"
    DESTINATION "share/fish/vendor_completions.d" COMPONENT shellext
    RENAME "f3d.fish")
  install(FILES "${F3D_SOURCE_DIR}/resources/app.f3d.F3D.metainfo.xml"
    DESTINATION "share/metainfo" COMPONENT assets)
endif()
